//
//  WowDaraSecondViewController.m
//  WowDara
//
//  Created by Siroj Chongkolwanont on 3/30/56 BE.
//  Copyright (c) 2556 Siroj Chongkolwanont. All rights reserved.
//

#import "WowDaraSecondViewController.h"
#import "Dara.h"
#import "DaraViewController.h"

@interface WowDaraSecondViewController ()

@end

@implementation WowDaraSecondViewController

NSMutableArray *daraArray;

Dara* currentDara;


id jsonObjectFromUrlString(NSString *urlString)
{
    NSURL *url = [NSURL URLWithString:urlString];
    NSError *error = nil;
    id jsonObject = nil;
    NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
    if(error)
        NSLog(@"%@", error);
    else
        jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    if(error)
        NSLog(@"%@", error);
    return jsonObject;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger row = [daraArray count];
    
    return row;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    NSInteger row = indexPath.row;
    
    Dara* dara = daraArray[row];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    // Here we use the new provided setImageWithURL: method to load the web image
    
    NSString *url_picture = @"http://119.59.127.95/~wowdara/";
    
    url_picture = [url_picture stringByAppendingString:dara.thrumb_url];
    
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url_picture]];
    
    
    // Display recipe in the table cell
    //Recipe *recipe = [recipes objectAtIndex:indexPath.row];
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    
    recipeImageView.image = [UIImage imageWithData:imageData];
    
    UILabel *recipeNameLabel = (UILabel *)[cell viewWithTag:102];
    recipeNameLabel.text = dara.name;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {
    NSInteger row = indexPath.row;
    currentDara = [daraArray objectAtIndex:row];
    
    
}


- (void)load_dara
{
    daraArray = [[NSMutableArray alloc] init];
    
    NSDictionary *publicTimeline =  jsonObjectFromUrlString(@"http://119.59.127.95/~wowdara/android/getDara.php");
    
    
    for(NSString *item in [publicTimeline allKeys]) {
        
        if([item isEqual: @"dara"] )
        {            
            NSArray *allDara = [publicTimeline valueForKey:item];
            
            for(NSDictionary *dara in allDara )
            {
                Dara* dara_obj = [[Dara alloc] init];
                dara_obj.id = [dara valueForKey:@"id"];
                dara_obj.name = [dara valueForKey:@"name"];
                dara_obj.thrumb_url = [dara valueForKey:@"thumbs_url"];
                dara_obj.image_url  = [dara valueForKey:@"images_url"];
                [daraArray addObject:dara_obj];
            }
            
        }
    }
    
    currentDara = [daraArray objectAtIndex:0];
}

- (void)viewDidLoad
{
    [self load_dara];
    
    self.navigationController.navigationBar.tintColor = [UIColor redColor];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed: @"nav_bg.png"]
                                                  forBarMetrics:UIBarMetricsDefault];
    
    [self.navigationController.navigationBar.topItem setTitleView:[[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"toolbar_logo.png"]]];
    
    UIButton *button1 = [[UIButton alloc] init];
    button1.frame= CGRectMake(0,0,43,32);
    [button1 setBackgroundImage:[UIImage imageNamed: @"nav_contact.png"] forState:UIControlStateNormal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
    
    
    [super viewDidLoad];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    
    //if([segue.identifier isEqualToString:@"ToSummary"])
    {
        DaraViewController *p = (DaraViewController*)segue.destinationViewController;
        p.dara = currentDara;
        //p.news_id = news_id;
        //p.feed = currentFeed;
    }
    
    
    
    
    
}

@end
